package page.pc.functionDetailPage;

import basicTool.WaitTool;
import dataBean.pc.overduequery.OverdueBean;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import page.pc.AbstractPCPage;

import java.util.List;

public class FunctionDetailPage extends AbstractPCPage {

    @FindBy(css = ".cat-banner.n-banner>.banner-text")
    private WebElement functionDetailListField;

    @FindBy(css = "a.cat-line-v.func-line")
    private List<WebElement> functionLinkList;

    @FindBy(css = ".due-control .due-search")
    private WebElement overdueQuerySubmitButton;

    @FindBy(css = "input[id='idnum']")
    private WebElement idCardOrMobileNumberFiled;

    public FunctionDetailPage(WebDriver driver) {
        super(driver);
        WaitTool.waitFor(driver, WaitTool.DEFAULT_WAIT_4_ELEMENT, functionDetailListField);
    }

    public FunctionDetailPage clickInvestmentLink() throws Exception {
        return click(getWebElementByName("投资", functionLinkList), FunctionDetailPage.class);
    }

    public FunctionDetailPage clickLendLink() throws Exception {
        return click(getWebElementByName("借钱", functionLinkList), FunctionDetailPage.class);
    }

    public FunctionDetailPage clickOverDueLink() throws Exception {
        return click(getWebElementByName("逾期查询", functionLinkList), FunctionDetailPage.class);
    }

    public <expectedPage extends AbstractPCPage> expectedPage setOverdueUserInfoToQueryInformation(OverdueBean overdueBean, boolean passOrBlock, String type) throws Exception {

        if (type.toLowerCase().equals("phonenumber")) {
            if (StringUtils.isNoneEmpty(overdueBean.getMobileNumber())) {
                setInputText(idCardOrMobileNumberFiled, overdueBean.getMobileNumber());
            }
        }
        if (type.toLowerCase().equals("idcard")) {
            if (StringUtils.isNoneEmpty(overdueBean.getIdCardNo())) {
            setInputText(idCardOrMobileNumberFiled, overdueBean.getIdCardNo());
            }
        }

        if (passOrBlock) {
            return (expectedPage) clickQueryButtonToGetOverdueInfo();
        }
        else {
            return (expectedPage) clickQueryButtonToCheckTheInputInfo();
        }
    }

    public SuccessGetResultPage clickQueryButtonToGetOverdueInfo() throws Exception {
        return click(overdueQuerySubmitButton, SuccessGetResultPage.class);
    }

    public ErrorToastPage clickQueryButtonToCheckTheInputInfo() throws Exception {
        return click(overdueQuerySubmitButton, ErrorToastPage.class);
    }

}
